package com.gitee.l0km.aocache.config;

import java.lang.reflect.Member;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.gitee.l0km.aocache.MemberKey;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 实现了 {@link CacheConfigSupplier} 接口的类，用于管理缓存配置。 该类使用 {@link ConcurrentMap}
 * 来存储和管理缓存配置，支持并发操作。
 * 
 * @author guadong
 */
public class ConcurrentMapCacheConfigSupplier implements CacheConfigSupplier {

	protected final ConcurrentMap<MemberKey, CacheConfig> configs = new ConcurrentHashMap<>();

	public ConcurrentMapCacheConfigSupplier() {
		super();
	}

	@Override
	public CacheConfig configOf(Member input, Object target) {
		if (null == input) {
			return null;
		}
		MemberKey key = MemberKey.of(input, target);
		return configs.get(key);
	}

	public CacheConfig put(Member input, Object target, CacheConfig config) {
		checkNotNull(input, "member can not be null");
		checkNotNull(config, "config can not be null");
		MemberKey key = MemberKey.of(input, target);
		return configs.put(key, config);
	}

	public CacheConfig remove(Member input, Object target) {
		if (null == input) {
			return null;
		}
		MemberKey key = MemberKey.of(input, target);
		return configs.remove(key);
	}

	public boolean containsKey(Member input, Object target) {
		if (null == input) {
			return false;
		}
		MemberKey key = MemberKey.of(input, target);
		return configs.containsKey(key);
	}

	public int size() {
		return configs.size();
	}

	public void clear() {
		configs.clear();
	}

}
